import { extendApi } from '@inbiz/utils';
export default extendApi(
  ( _events,
    {
      state,
      listData,
      $listData,
      props,
      valueJson,
      storageField,
      selectData
    }
  ) => {
    return {
      getSaveData: () => {
        const storageField = props.showconfig?.storageField || 'id';
        let propsValue = props.value;
        if (props.childModelConfig) {
          let dataList: any[] = [];
          if (typeof propsValue == 'number') {
            propsValue = String(propsValue);
          }
          if (typeof propsValue == 'string' && propsValue !== '') {
            propsValue = propsValue.split(',');
            propsValue = propsValue.map((item: string) => item.trim());
          }
          if (Array.isArray(propsValue)) {
            let arr = listData.list || [];
            if (!arr?.length) {
              if (valueJson?.length) {
                arr = valueJson.map((item: string) => {
                  if (typeof item === 'string') {
                    return JSON.parse(item);
                  } else {
                    return item;
                  }
                });
              }
            }
            if (!Array.isArray(arr)) {
              arr = [arr];
            }
            dataList = propsValue?.map((item: string) => {
              let fil = arr?.filter((val: any) => val?.[storageField] == item);
              return props.storageConfig?.tableData.reduce((pre: any, next: any) => {

                pre[next.modelAttr] = fil?.[0]?.[next.componentAttr];
                pre['_opt'] = 'add';
                return pre;
              }, {});
            });
          }
          const data = {
            value: Array.isArray(propsValue) ? propsValue.join(',') : propsValue,
            childData: props.storageConfig
              ? {
                  controlId: props.controlId,
                  modelKey: props.storageConfig?.business,
                  datas: [
                    ...dataList,
                    {
                      _opt: 'del',
                    },
                  ],
                }
              : undefined,
          };
          return data;
        }
        return {
          value: Array.isArray(propsValue) ? propsValue.join(',') : propsValue,
        };
      },
      loadData: (data: { list: any[]; pageIndex: number; total: number }) => {
        $listData(data);
      },
      getData: () => {
        return listData;
      },
      getText: () => {
        if (props.selecttype) {
          if(state?.checkList?.length){
            return state?.checkList?.map(item => selectData.find(data => data?.[storageField] === item)?.[props.showconfig.selectName]).join(',');
          }
        }
        return selectData?.find(data => data?.[storageField] === state.checkList)?.[props.showconfig.selectName];
      },
      getSelect: () => {
        if (props.selecttype) {
          return state?.checkList?.map(item => selectData?.find(data => data?.[storageField] === item));
        }
        return selectData?.find(data => data?.[storageField] === state.checkList);
      },
    };
  },
);

